<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');



require_once(APPPATH .'/libraries/REST_Controller.php');



/**
 * class Tree
 * Will be the controller for all tree actions within the vta application.
 *
 * @author      Jeroen Sen <jeroensen@22vision.nl>
 * @version     20120814
 *
 */
class Tree extends REST_Controller {



    /**
     * public function all_get
     * Will act a gateway for a GET request, returns all soil objects knonw in the database sorted by Name DESC.
     *
     * @author      Jeroen Sen <jeroensen@22vision.nl>
     * @version     20120814
     * @param
     * @return      void
     *
     */
    public function all_get()
    {
        $criteria = array();
        $orderBy = array('id' => 'ASC');

        $trees = $this->doctrine->em->getRepository('Entities\Tree')->findBy($criteria, $orderBy);

        $data = array();

        foreach ($trees as $index => $tree) {
            $data[] = array('id' => $tree->getId(),
                            'treespecie_id' => $tree->getTreespecie()->getId(),
                            'treespecie_latinname' => $tree->getTreespecie()->getLatinname(),
                            'location_id' => $tree->getLocation()->getId(),
                            'location_longitude' => $tree->getLocation()->getLongitude(),
                            'location_latitude' => $tree->getLocation()->getLatitude(),
                            'location_street' => $tree->getLocation()->getStreet(),
                            'location_city' => $tree->getLocation()->getCity()
                            );
        }

        $this->response(array('data' => $data));

    } // public function all_get()



    /**
     * public function create_put
     * Will add another Soil to the database based on a PUT request.
     *
     * @author      Jeroen Sen <jeroensen@22vision.nl>
     * @version     20120808
     * @param       void, working of the member Array $this->put
     * @return      String true
     *
     */
    public function create_put()
    {
error_log(__METHOD__);
error_log(print_r($this->put('location'), true));
error_log(print_r($this->put('plantdate'), true));

// error_log(print_r($datetime, true));

error_log(print_r($this->put('multitrunk')));


        $tree = new Entities\Tree;

        $employee = $this->doctrine->em->find('Entities\Employee', 1);
        $treespecie = $this->doctrine->em->find('Entities\Treespecie', intval($this->put('treespecieId')));

        $locationData = $this->put('location');
        $location = $this->doctrine->em->find('Entities\Location', intval($locationData['id']));

        $soil = $this->doctrine->em->find('Entities\Soil', intval($this->put('soilId')));

        $plantdate = new DateTime($this->put('plantdate'), new DateTimeZone('Europe/Amsterdam'));

        $tree->setNumber($this->put('number'));
        $tree->setTreespecie($treespecie);
        $tree->setEmployee($employee);
        $tree->setLocation($location);
        $tree->setSoil($soil);
        $tree->setPlantdate($plantdate);
        $tree->setStemdiameter(intval($this->put('stemdiameter')));
        $tree->setHeight(intval($this->put('height')));
        $tree->setMultitrunk(intval($this->put('multitrunk')));
        $tree->setValuable(intval($this->put('valuable')));
        $tree->setIconic(intval($this->put('iconic')));


        $this->doctrine->em->persist($tree);
        $this->doctrine->em->flush();
error_log(print_r($tree, true));
        $data = array('id' => $tree->getId(),
                'number' => $tree->getNumber(),
                'treespecie' => $tree->getTreespecie()->getId(),
                'employee' => $tree->getEmployee()->getId(),
                'location' => $tree->getLocation()->getId(),
                'soil' => $tree->getSoil()->getId(),
                'plantdate' => $tree->getPlantdate(),
                'stemdiameter' => $tree->getStemdiameter(),
                'height' => $tree->getHeight(),
                'multitrunk' => $tree->getMultitrunk(),
                'valuable' => $tree->getValuable(),
                'inconic' => $tree->getIconic());
error_log(print_r($data, true));
        $this->response(array('data' => $data));

    } // public function create_put()



    /**
     * public function update_post
     * Will update a single Soil object based on a POST request. Works of the member variable Array $this->post.
     * curl -X POST -H "Accept: application/json" -d '{"id":666}' http://zichtopbomen.local/vta/soil/edit
     *
     * @author      Jeroen Sen <jeroensen@22vision.nl>
     * @version     20120808
     * @param       void
     * @return      String true
     *
     */
    public function update_post()
    {
        $id = intval($this->post('id'));
        $active = false;

        if ($this->post('active') == 'true') {
            $active = true;
        }


        $soil = $this->doctrine->em->find('Entities\Soil', $id);

        $soil->setName($this->post('name'));
        $soil->setActive($active);

        $persist = $this->doctrine->em->persist($soil);
		$test = $this->doctrine->em->flush();

error_log($this->post('id'));
error_log($this->post('name'));
error_log($this->post('active'));
error_log(gettype($this->post('active')));

        $this->response('true');

    } // public function update_post()



    /**
     *
     *
     */
    public function test_get()
    {
        $this->response(array('data' => 'dit is een test'));

    } //public function test_get()




} // class Vta extends CI_Controller